﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Bussiness.Manager;
using System.Data;
using System.Windows.Forms;
namespace DSC
{
    public class SessionLogin
    {
        public static int UserId = 0;
        public static string UserCode = "";
        public static string UserName = "";
        public static string Role="";
        public static int RoleId = 0;
        public static string DEALER_CODE = "";
        public static List<RoleFunction> lstRoleFunction = new List<RoleFunction>();

        /// <summary>
        /// Load role cua cac user vao buffer
        /// </summary>
        public static void LoadRoleOfUser()
        {
            //lay danh sach cac form
            DataTable tblForm = PT_Role_FunctionManager.CreateInstant().GetAllForm(RoleId);
            //lay danh sach cac tag trong form
            DataTable tblRole = PT_Role_FunctionManager.CreateInstant().LoadFunction(RoleId);
            DataView vwRole = tblRole.DefaultView;
            //dua vao danh sach cac role
            foreach (DataRow r in tblForm.Rows)
            {
                vwRole.RowFilter = "FormName='" + r["FormName"] +"'";
                RoleFunction role = new RoleFunction();
                role.FormName = r["FormName"].ToString();
                foreach (DataRowView v in vwRole)
                    role.hshFunction.Add(v["Tag"], true);
                lstRoleFunction.Add(role);          
            }

        }


        public static RoleFunction GetRoleFunctionOfForm(string formName)
        {
           // return new RoleFunction();
            for (int i = 0; i < lstRoleFunction.Count; i++)
                if (lstRoleFunction[i].FormName.ToUpper() == formName.ToUpper())
                    return lstRoleFunction[i];
            return null;
        }

        public static void CheckPermission(string formName, Control ctl)
        {
            RoleFunction role = GetRoleFunctionOfForm(formName);
            if (role != null)
            {
                foreach (Control ctlChild in ctl.Controls)
                {
                    if (ctlChild.Tag != null && !string.IsNullOrEmpty(ctlChild.Tag.ToString()))
                    {
                        if (!role.HasFunction(ctlChild.Tag.ToString()))
                            ctlChild.Enabled = false;
                    }
                    CheckPermission(formName, ctlChild);
                }
            }
            else
            {
                foreach (Control ctlChild in ctl.Controls)
                {
                    if (ctlChild.Tag != null && !string.IsNullOrEmpty(ctlChild.Tag.ToString()))
                    {                       
                            ctlChild.Enabled = false;
                    }
                    CheckPermission(formName, ctlChild);
                }
            }
        }


    }

    public class RoleFunction
    {
        public string FormName = "";
        public Hashtable hshFunction = new Hashtable();
        public bool HasFunction(string functionName)
        {
            return hshFunction.ContainsKey(functionName);
        }
    }
}
